/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.GUI.sidepanels;

import controller.Constants;
import controller.commands.EditNodeCommand;
import controller.commands.SetVisibleCommand;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import model.graphic.locations.Location;
import model.graphic.objects.GNode;
import model.graphic.objects.Graphical;
import model.graphic.objects.RectangleAttachment;
import view.GUI.LabelReader;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Arfeniel
 */
public class PropertiesTableModel extends AbstractTableModel implements TableModelListener {

    private String[] columnNames;
    private Object[][] data;
    private Graphical g;
    private GraphicView gv;

    PropertiesTableModel(String[] columnNames, Object[][] data, Graphical g) {
        this.columnNames = columnNames;
        this.data = data;;
        this.g = g;
        this.gv = (GraphicView)g.getParent();
        this.addTableModelListener(this);
    }

    public int getRowCount() {
        return data.length;
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        return !(col == 0 ||(row == 2));
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }

    public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel) e.getSource();
        Object datavalue = model.getValueAt(row, column);
        String naamattr = (String) model.getValueAt(row, column - 1);
        if (naamattr.equals(LabelReader.getInstance().getString("Name"))) {
            JLabel l= (JLabel) datavalue;
            g.setName(l.getText());
        } else if (naamattr.equals(LabelReader.getInstance().getString("Location"))) {
            Location l = (Location) datavalue;
            Location nodel = g.getGraphLocation();
            nodel.setXY(l.getX(),l.getY());
            g.setLocation(nodel);
        } else if (naamattr.equals(LabelReader.getInstance().getString("BaseColor"))) {
            Color c = (Color) datavalue;
            g.setBaseColor(c);
       /* } else if (naamattr.equals(LabelReader.getInstance().getString("BackColor"))) {
            Color c = (Color) datavalue;
            g.setBackColor(c);*/
        }else if (naamattr.equals(LabelReader.getInstance().getString("Visible"))) {
            gv.notifyObservers(new SetVisibleCommand(g,(Boolean) datavalue));
        }else if (naamattr.equals(LabelReader.getInstance().getString("HighLightColor"))) {
            Color c = (Color) datavalue;
            g.setHighLightColor(c);
        } else if (naamattr.equals(LabelReader.getInstance().getString("StoreValue"))) {
            String s = (String) datavalue;
            GNode gn = (GNode) g;
            gv.notifyObservers(new EditNodeCommand(Constants.getValueString(), gn, s));
        } else if (naamattr.equals(LabelReader.getInstance().getString("Collapse"))) {
            ((RectangleAttachment)g).collapse((Boolean)datavalue);
            //gv.notifyObservers(new SetVisibleCommand(g,(Boolean) datavalue));
        }
        gv.repaint();
    }
}
